import { _decorator, Component, Node, Label } from 'cc';
import { Utitls } from '../Utils';
const { ccclass, property } = _decorator;

@ccclass('TimeController')
export class TimeController extends Component {

    private callBack: Function
    @property(Label)
    private timeLabel: Label = null;

    private timeValue: number = 0;

    OnInit(time, call) {
        this.callBack = call;
        this.timeValue = time;
    }

    PlayTime() {
        let myThis = this;
        this.schedule(function () {
            myThis.timeValue = myThis.timeValue - 1;
            const decomposed1 = Utitls.TimestampToDHMS(myThis.timeValue * 1000);
            myThis.timeLabel.string = Utitls.FormatDHMSTo2(decomposed1);
            if (myThis.timeValue <= 0) {
                myThis.timeLabel.string = "00:00:00";
                myThis.StopTime();
                if (myThis.callBack) {
                    myThis.callBack();
                }
            }
        }.bind(this), 1);
    }

    StopTime() {
        this.unscheduleAllCallbacks();
    }

}

